package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.common.enums.WarnSortFieldType;
import com.econ.powercloud.ems.dal.entity.EmsWarnStatistical;
import com.econ.powercloud.ems.dal.mapper.EmsWarnStatisticalMapper;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @Author : zjf
 * @Description :
 * @Date : 2022/12/2 11:24
 * @Modified by : zjf
 */
@Repository
public class EmsWarnStatisticalDAO extends ServiceImpl<EmsWarnStatisticalMapper, EmsWarnStatistical> {

    public Page<EmsWarnStatistical> selectPageByDeviceCode(String deviceCode,boolean isAsc,Integer sortField,
                                                           Integer pageNo,Integer pageSize){
        LambdaQueryWrapper<EmsWarnStatistical> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotBlank(deviceCode),EmsWarnStatistical::getDeviceCode,deviceCode);

        wrapper.orderBy(true,isAsc, Objects.equals(sortField, WarnSortFieldType.WARN_TOTAL_NUM.getCode())?EmsWarnStatistical::getWarnTotalNum:
                Objects.equals(sortField, WarnSortFieldType.WARN_TODAY_PID_NUM.getCode())?EmsWarnStatistical::getWarnTodayPidNum: EmsWarnStatistical::getWarnTodayNum);
        wrapper.orderBy(true,true,EmsWarnStatistical::getSort);
        Page<EmsWarnStatistical> page = new Page<>(pageNo,pageSize);
        return this.baseMapper.selectPage(page,wrapper);
    }

    public EmsWarnStatistical selectOneByDeviceCode(String deviceCode){
        LambdaQueryWrapper<EmsWarnStatistical> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EmsWarnStatistical::getDeviceCode,deviceCode);
        wrapper.last("limit 1");
        return this.baseMapper.selectOne(wrapper);
    }
}
